/**
 * Created by Administrator on 2017/10/5 0005.
 */
(function ($j) {
    function init() {
        renderData();
        bindBtnEvent();
    }

    function renderData() {
        $.showPreloader();
        $('.name').val('');
        $('.phone').val('');
        $j.restPost('/referrer/doGetReferrerCount').done(function(mess,data){
            $('.installCount').html(data.installCount);
            $('.unInstallCount').html(data.unInstallCount);
            $.hidePreloader();
        }).fail(function(mess){
            $.toast(mess);
            $.hidePreloader();
        })
    }

    function bindBtnEvent() {
        $('.installed,.uninstalled').off('click').on('click', function () {
            window.location.href = "/user/recommendList?tab="+$(this).attr('class');
        });

        $('.save').off('click').on('click', function () {
            var name = $('.name').val();
            var phone = $('.phone').val();
            if (name == '') {
                $.toast('姓名不能为空');
                return false;
            }
            if (phone == '') {
                $.toast('手机号码不能为空');
                return false;
            }

            if(!$j.IsTel(phone)){
                $.toast('手机号码格式有误');
                return false;
            }

            var param = {
                name: name,
                phone: phone
            };

            $j.restPost('/referrer/doSelect', param).done(function (mess, data) {
                if (!data) {
                    $.alert('该用户已被推荐过,请重新输入',function(){
                        init();
                    })
                }else{
                    $j.restPost('/referrer/doInsert', param).done(function (mess, data) {
                        $.alert('推荐成功', function () {
                            init();
                        });
                    }).fail(function (mess) {
                        $.toast(mess);
                    });
                }
            }).fail(function (mess) {
                $.toast(mess);
            })
        });

        $('.cancel').off('click').on('click', function () {
            $.router.back();
        });
    }

    init();
})(jQuery);